package project.layer.ant;

import java.util.ArrayList;

import project.data.EvaporableData;
import project.data.FeasibilityData;

import framework.layer.TickListener;

public class AntLayer implements TickListener{
	
	private static AntLayer instance = new AntLayer();
	private final int TURNOVER_COUNT = 20;
	private ArrayList<Ant> ants = new ArrayList<Ant>();
	private ArrayList<Ant> antsToBeAdded = new ArrayList<Ant>();
	private ArrayList<Ant> antsToBeRemoved = new ArrayList<Ant>();
	private ArrayList<EvaporableData> evaporableData = new ArrayList<EvaporableData>();
	private ArrayList<EvaporableData> evaporableDataToBeAdded = new ArrayList<EvaporableData>();
	
	private ArrayList<EvaporableData> evaporableDataToBeRemoved = new ArrayList<EvaporableData>();
	
	
	private int ticksLeft;


	public ArrayList<EvaporableData> getEvaporableDataList() {
		return evaporableData;
	}

	public static AntLayer getInstance(){
		if(instance != null){
			return instance;
		}
		else{
			return instance = new AntLayer();
		}
	}
	
	private AntLayer(){
		
	}
	
	public ArrayList<EvaporableData> getEvaporableDataToBeAdded() {
		return evaporableDataToBeAdded;
	}

	public ArrayList<EvaporableData> getEvaporableDataToBeRemoved() {
		return evaporableDataToBeRemoved;
	}
	
	private int getNumberOfSearchingExplorationAnts(){
		int nrOfSearchingExplorationAnts = 0;
		for(Ant ant : getAnts()){
			if(ant instanceof SearchingExplorationAnt){
				nrOfSearchingExplorationAnts++;
			}
		}
		return nrOfSearchingExplorationAnts;
	}
	private int getNumberOfFollowingExplorationAnts(){
		int nrOfFollowingExplorationAnts = 0;
		for(Ant ant : getAnts()){
			if(ant instanceof FollowingExplorationAnt){
				nrOfFollowingExplorationAnts++;
			}
		}
		return nrOfFollowingExplorationAnts;
	}
	private int getNumberOfFeasibilityAnts(){
		int nrOfFeasibilityAnts = 0;
		for(Ant ant : getAnts()){
			if(ant instanceof FeasibilityAnt){
				nrOfFeasibilityAnts++;
			}
		}
		return nrOfFeasibilityAnts;
	}
	private void processAntTick(){
		registerListeners();
		unregisterListeners();

		for(Ant ant : getAnts()){
			ant.processAntTick();
		}
		for(EvaporableData data : getEvaporableDataList()){
			data.processAntTick();
		}

	}

	

	@Override
	public void processTick(long timePassed) {
		if (ticksLeft == 0) {
			processAntTick();
			ticksLeft = TURNOVER_COUNT;
		}
		ticksLeft--;

	}
	private void registerListeners(){
		getAnts().addAll(getAntsToBeAdded());
		getAntsToBeAdded().clear();
		getEvaporableDataList().addAll(getEvaporableDataToBeAdded());
		getEvaporableDataToBeAdded().clear();
		
	}
	private void unregisterListeners(){
		getAnts().removeAll(getAntsToBeRemoved());
		getAntsToBeRemoved().clear();
		getEvaporableDataList().removeAll(getEvaporableDataToBeRemoved());
		getEvaporableDataToBeRemoved().clear();
	}
	public boolean registerAnt(Ant ant){
		return getAntsToBeAdded().add(ant);
	}
	
	public boolean unregisterAnt(Ant ant){
		return getAntsToBeRemoved().add(ant);
	}
	
	public void registerEvaporableData(EvaporableData data){
		getEvaporableDataToBeAdded().add(data);
		
	}
	
	public void unregisterEvaporableData(EvaporableData data){
		getEvaporableDataToBeRemoved().add(data);
	}
	
	

	public ArrayList<Ant> getAnts() {
		return ants;
	}

	public ArrayList<Ant> getAntsToBeAdded() {
		return antsToBeAdded;
	}
	public ArrayList<Ant> getAntsToBeRemoved() {
		return antsToBeRemoved;
	}
}
